<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends Controller{
	public function _initialize(){
		if(!session('?admin')){
			$this->error('当前用户没有权限','Login/login');
			exit;
		}
	}

    public function index(){
    echo 'this is admin module';
    }
    public function userList(){
    	$userModel=M('users');
    	$users=$userModel->order('id desc')->select();
        $this->users=$users;
        //$this->assign();
        $this->display();
    }
       public function bsUserList(){
       	$User = M('Users'); // 实例化User对象
$count = $User->count();// 查询满足要求的总记录数
$Page = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(25)
$show = $Page->show();// 分页显示输出
// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
$list = $User->limit($Page->firstRow.','.$Page->listRows)->select();
//$this->users=$list;
//$this->Page=$show;
$this->assign('users',$list);// 赋值数据集
$this->assign('page',$show);// 赋值分页输出
$this->display(); // 输出模板
    	//$userModel=M('users');
    	//$users=$userModel->order('id desc')->select();
       // $this->users=$users;
        //$this->assign();
       // $this->display();
    }
        public function users(){
    	$userModel=M('users');
    	$users=$userModel->order('id desc')->select();
        $this->users=$users;
        //$this->assign();
        $this->display();
    }
    function del($id){
    	//生成一个uesrs模型，调用模型的方法删除数据
    	$usersModel=M('users');
    	if($usersModel->delete($id)){
    	//delete from think_users where id=$id 
    		//删除成功
    		$this->success('删除成功');
    	}else{
    		//删除失败
    		$this->error('删除失败');
    	}
    }
    public function setstar($id){
    	//生成一个users表对应的模型实例usersModel
    	$usersModel=M('users');
    	$data['isstar']=1;
    	if($usersModel->where("id=$id")->save($data)){
    		$this->success('修改成功');
    	}else{
    		$this->error('修改失败');
    	}
    	//update users set isstar=1 where id=$id;
    }
    function unstar($id){
    	$usersModel=M('users');
    	$usersModel->isstar=0;
    	$usersModel->isfirst=0;
    	$usersModel->id=$id;
    	if($usersModel->save()){
    		$this->success('取消明星作者成功');
    	}else{
    		$this->error('取消明星作者失败');
    	}
    }

    function setfirst($id){
    	//生成一个users表对应的模型实例usersModel
    	$usersModel=M('users');
    	$usersModel->isstar=1;
    	$usersModel->isfirst=1;
    	$usersModel->id=$id;
    	if($usersModel->save()){
    		$this->success('设置首页推荐成功');
    	}else{
    		$this->error('设置首页推荐失败');
    	}
    	//update users set isstar=1 where id=$id;
    }
    function unfirst($id){
    	$usersModel=M('users');
    	$usersModel->isfirst=0;
    	$usersModel->id=$id;
    	if($usersModel->save()){
    		$this->success('取消首页推荐成功');
    	}else{
    		$this->error('取消首页推荐失败');
    	}
    }
    function updateUserInfo($id){
    	$usersModel=M('users');
    	$this->userInfo=$usersModel->find($id);
    	$this->display();
    
    	//将查询到的user信息传递给模板updateUserInfo.html
    }

    function update(){
    	//获取updateUserInfo界面提交过来的数据，写入修改数据库
    	//$data=$_POST
    	//$data=I(post.)
    	//create() 利用￥——POST数据创建一个对象
    	

    	$userModel=D('users');
        $userModel->create();
         if($_FILES['photo']['error']==0){
         $photoinfo=$this->upload();
         $userModel->photo=$photoinfo['photo']['savename'];
        	}
    	if($userModel->save()){
    		$this->success('修改用户成功','users');

	    	}else{
	    		$this->error('修改用户失败');
	    	}
   		 //}
    
    	
    }

   public function upload(){
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728 ;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Public/Uploads/photo/'; // 设置附件上传根目录
        //$upload->savePath = ''; // 设置附件上传（子）目录
        $upload->autoSub=false;
        // 上传文件
        $info = $upload->upload();
        if(!$info) {// 上传错误提示错误信息
        $this->error($upload->getError());
        }else{// 上传成功
             return $info;
      // $this->success('上传成功！');
        }
    }
    function img(){
         $photoinfo=$this->upload();
         $filePath= './Public/Uploads/photo/';
         //$imgfile=$photoinfo['photo']['rootpath'].'\\'.$photoinfo['savephoto']['savename'];

         $oldFileName=$photoinfo['photo']['savename'];
         $oldFileFullName=$filePath.'\\'.$oldFileName;

         $newFileName='thumb_'.$oldFileName;
         $newFileFullName=$filePath."\\".$newFileName;

         $image=new \Think\Image();
         $image->open($oldFileFullName);
         $image->thumb(100,100)->save($newFileFullName);
         return $newFileName;
        }
        

public function addUser(){
	//$data=$_POST
//$data=I(post.)
	//create() 利用￥——POST数据创建一个对象
	//2.单独处理图像的上传  以及photo字段的赋值
	//3.保存到数据库 add()

    	//获取updateUserInfo界面提交过来的数据，写入修改数据库
    	//$data=$_POST
    	//$data=I(post.)
    	//create() 利用￥——POST数据创建一个对象
    	$userModel=D('Users');
        if($userModel->create()){
        	 if($_FILES['photo']['error']==0){
        	  $fileInfo=$this->img();
               //$photoinfo=$this->upload();
               $userModel->photo=$fileInfo;
    	     if($userModel->add()){
    		   $this->success('添加用户成功','users');

    	     }else{
    		      $this->error('添加用户失败');
    	     }
        }
        }else{
    	$this->error($userModel->getError());
    }
    }
}

   

